clear all

cd "~/Dropbox (Harvard University)/Monetary Policy with Heterogenous Producers/Replication Code"

ssc install grstyle, replace
grstyle init
grstyle set plain, nogrid
graph set window fontface "Palatino"
grstyle set size 16pt: subheading axis_title
grstyle set size 14pt: tick_label key_label

use "data/data_temp/aggregated_sales_list", clear
rename quarter dt
g date = dofc(dt)
g quarter = qofd(date)
format quarter %tq
keep quarter naics3 sale_ms4 saleq n_firms
xtset naics3 quarter
save "data/data_temp/aggregated_sales_list_clean", replace

local mu_list mu_li_ms4 mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	use "data/data_temp/aggregated_`mu'_quarter", clear
	rename quarter dt
	g date = dofc(dt)
	g quarter = qofd(date)
	g year = yofd(date)
	format quarter %tq
	merge m:1 year quarter using "data/data_temp/quarterly_aggregates_clean"
	drop if _m!=3
	drop _merge
	*drop if year>=2008
	tsset quarter 
		
	local hmin = -5
	local hmax = 12
	forvalues t = `hmin'/`hmax' {
		local str_t = `t'
		if `t' < 0 {
			local abs_t = -1 * `t' 
			local str_t = "m`abs_t'"
		}
		g cost_alt_cov_`mu'_`str_t' = sales_cov_`mu'_`str_t' - dlogmu_cov_`mu'_`str_t' 
		*g costq_alt_cov_`mu'_`str_t' = saleq_cov_`mu'_`str_t' - dlogmu_cov_`mu'_`str_t'
	}

	// Local projections: Covariance of markups and change in markups
	local hmax = 12
	local cycle_var resid_full
	local depvar_list dlogmu_cov cost_alt_cov 
	foreach depvar in `depvar_list' {
		forvalues h = 0/`hmax' {
			qui newey `depvar'_`mu'_`h' L(0/4).`cycle_var' `depvar'_`mu'_m1 `depvar'_`mu'_m2 `depvar'_`mu'_m3 `depvar'_`mu'_m4, lag(4)
			eststo p`h'
		}
		local ylabel = "Dependent var"
		if ("`depvar'"=="dlogmu_cov") | ("`depvar'"=="dlogmu_cov_w1") {
			local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log{&mu}) (%)"
		}
		if ("`depvar'"=="sales_cov") | ("`depvar'"=="sales_cov_w1") {
			local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log sales) (%)"
		}
		if ("`depvar'"=="cost_cov") | ("`depvar'"=="costq_cov") | ("`depvar'"=="cost_cov_w1") | ("`depvar'"=="cost_alt_cov") | ("`depvar'"=="costq_alt_cov") {
			local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log costs) (%)"
		}
		if ("`depvar'"=="logmu_var") {
			local ylabel = "Var{sub:{&lambda}}(log{&mu}) (%)"
		}
		coefplot (p*), vertical keep(`cycle_var') asequation swapnames eqrename(^p(.*)$ = \1, regex) color("31 119 180") yline(0) legend(off) xtitle("Quarters after monetary shock") ytitle(`ylabel') recast(connected) levels(68 95) ciopts(recast(rarea rconnected) color(%20 gray%50) lwidth(0) msymbol(i i) lpattern(dash dash)) ysize(1) xsize(2) rescale(100)
		gr rename fig_`depvar'_`mu', replace
		local cycle_name = ""
		if `cycle_var'!=resid_full {
			local cycle_name = "_`cycle_var'"
		}
		graph export "outputs/inv_`mu'_`depvar'`cycle_name'.pdf", replace fontface("Palatino")
	}
}

*** WITHIN INDUSTRY
 

local mu_list mu_li_ms4 mu_cfg1_ms4 mu_cfg2_ms4
local ind_list naics3
foreach mu in `mu_list' {
	foreach ind_var in `ind_list' {
		*** For sample, replace
		*local mu mu_cfg1_ms4
		*local ind_var naics3
		*** End replace
		use "data/data_temp/aggregated_`mu'_quarter`ind_var'", clear
		rename quarter dt
		g date = dofc(dt)
		g quarter = qofd(date)
		g year = yofd(date)
		format quarter %tq
		merge m:1 year quarter using "data/data_temp/quarterly_aggregates_clean"
		drop if _m!=3
		drop _merge
		*tostring naics3, gen(naics3_str)
		*g naics2 = substr(naics3_str, 1, 2)
		*local ind_var_cluster naics2
		
		merge 1:1 naics3 quarter using "data/data_temp/aggregated_sales_list_clean"
		drop if _m!=3
		drop _merge
				
		local hmin = -5
		local hmax = 12
		forvalues t = `hmin'/`hmax' {
			local str_t = `t'
			if `t' < 0 {
				local abs_t = -1 * `t' 
				local str_t = "m`abs_t'"
			}
			g cost_alt_cov_`mu'_`str_t' = sales_cov_`mu'_`str_t' - dlogmu_cov_`mu'_`str_t' 
		}

		local hmax = 12
		local cycle_var resid_full
		local depvar_list dlogmu_cov cost_alt_cov 
		xtset `ind_var' quarter
		foreach depvar in `depvar_list' {
			forvalues h = 0/`hmax' {
				qui xtreg `depvar'_`mu'_`h' L(0/4).`cycle_var' `depvar'_`mu'_m1 `depvar'_`mu'_m2 `depvar'_`mu'_m3 `depvar'_`mu'_m4 if n_firms>=5, fe
				if `h'==0 {
					disp `h'
					qui regress `depvar'_`mu'_`h' L(0/4).`cycle_var' `depvar'_`mu'_m1 `depvar'_`mu'_m2 `depvar'_`mu'_m3 `depvar'_`mu'_m4 if n_firms>=5
				}
				eststo p`h'
			}
			local ylabel = "Dependent var"
			if ("`depvar'"=="dlogmu_cov") | ("`depvar'"=="dlogmu_cov_w1") {
				local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log{&mu}) (%)"
			}
			if ("`depvar'"=="sales_cov") | ("`depvar'"=="sales_cov_w1") {
				local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log sales) (%)"
			}
			if ("`depvar'"=="cost_cov") | ("`depvar'"=="costq_cov") | ("`depvar'"=="cost_cov_w1") | ("`depvar'"=="cost_alt_cov") | ("`depvar'"=="costq_alt_cov") {
				local ylabel = "Cov{sub:{&lambda}}(-1/{&mu}, {&Delta}log costs) (%)"
			}
			if ("`depvar'"=="logmu_var") {
				local ylabel = "Var{sub:{&lambda}}(log{&mu}) (%)"
			}
			coefplot (p*), vertical keep(`cycle_var') asequation swapnames eqrename(^p(.*)$ = \1, regex) color("31 119 180") yline(0) legend(off) xtitle("Quarters after monetary shock") ytitle(`ylabel') recast(connected) levels(68 95) ciopts(recast(rarea rconnected) color(%20 gray%50) lwidth(0) msymbol(i i) lpattern(dash dash)) ysize(1) xsize(2) rescale(100)
			gr rename fig_`depvar'_`mu', replace
			local cycle_name = ""
			if `cycle_var'!=resid_full {
				local cycle_name = "_`cycle_var'"
			}
			*graph export "outputs/inv_`mu'_`depvar'_`ind_var'`cycle_name'.pdf", replace fontface("Palatino")
		}
	}
}
